package com.briup.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 使用jdbc 查询数据
 */
public class SelectData {
    public static void main(String[] args) {
        List<Emp> list=new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "bttc2", "bttc2");
            statement = connection.createStatement();
            String sql = "select id,last_name,salary,title from s_emp";
            //执行select 语句的时候 要使用executeQuery
            resultSet = statement.executeQuery(sql);
            //处理结果集 resultSet代指一行一行的数据 默认在第一行之前
            while (resultSet.next()) {
                // 获取一行中的第一列数据  获取id列
                int id = resultSet.getInt(1);
                id = resultSet.getInt("id");
                String last_name = resultSet.getString("last_name");
                int salary = resultSet.getInt("salary");
                String title = resultSet.getString("title");
                Emp emp = new Emp(id, last_name, salary, title);
                list.add(emp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (connection!=null)
                    connection.close();
                if (statement!=null)
                    statement.close();
                if (resultSet!=null)
                    resultSet.close();
            }catch (Exception e){

            }
            list.forEach(System.out::println);
        }
    }
}
